package com.atguigu.controller;


import com.alibaba.fastjson.JSONObject;
import com.atguigu.entity.BaseCategory1;
import com.atguigu.entity.BaseCategory2;
import com.atguigu.entity.BaseCategory3;
import com.atguigu.entity.BaseCategoryView;
import com.atguigu.result.RetVal;
import com.atguigu.service.BaseCategory1Service;
import com.atguigu.service.BaseCategory2Service;
import com.atguigu.service.BaseCategory3Service;
import com.atguigu.service.BaseCategoryViewService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 一级分类表 前端控制器
 * </p>
 *
 * @author linjiang
 * @since 2022-07-20
 */
@RestController
@RequestMapping("/product")
@Api(tags = "商品分类接口")
public class CategoryController {
    @Autowired
    private BaseCategory1Service baseCategory1Service;

    @Autowired
    private BaseCategory2Service baseCategory2Service;

    @Autowired
    private BaseCategory3Service baseCategory3Service;
    @Autowired
    private BaseCategoryViewService baseCategoryViewService;

    //一级
    //http://127.0.0.1/product/getCategory1/2
    @ApiOperation("获取一级分类")
    @GetMapping("getCategory1")
    public RetVal getCategory1(){
       List<BaseCategory1>  baseCategory1List=baseCategory1Service.list(null);
       return RetVal.ok(baseCategory1List);
    }
    //二级
    //http://127.0.0.1/product/getCategory2/2
    @ApiOperation("获取二级分类")
    @GetMapping("getCategory2/{baseCategory1Id}")
    public RetVal getCategory2(@PathVariable("baseCategory1Id") Long baseCategory1Id){
        QueryWrapper<BaseCategory2> wrapper = new QueryWrapper<>();
        wrapper.eq("category1_id",baseCategory1Id);
        List<BaseCategory2> baseCategory2List = baseCategory2Service.list(wrapper);
        return RetVal.ok(baseCategory2List);
    }
    //三级
    //http://127.0.0.1/product/getCategory3/2
    @ApiOperation("获取三级分类")
    @GetMapping("getCategory3/{baseCategory2Id}")
    public RetVal getCategory3(@PathVariable("baseCategory2Id") Long baseCategory2Id){
        QueryWrapper<BaseCategory3> wrapper = new QueryWrapper<>();
        wrapper.eq("category2_id",baseCategory2Id);
        List<BaseCategory3> baseCategory3List = baseCategory3Service.list(wrapper);
        return RetVal.ok(baseCategory3List);
    }

    @ApiOperation("获取前端分类列表")
    @GetMapping("getIndexCategory")
    public RetVal getIndexCategory(){
        List<JSONObject>  categoryViewList= baseCategoryViewService.getIndexCategory();
        return RetVal.ok(categoryViewList);
    }

}

